const {tokenERR}=require('../errType/err.type')
const jwt = require("jsonwebtoken");
// 是否登录认证
const auth=async (ctx,next)=>{
    const {authorization}=ctx.request.header
    const token=authorization.replace('Bearer ',"")
    try{
        var user=jwt.verify(token,'zddata')
        ctx.state.user=user
    }catch (e) {
        switch (e.name){
            case 'TokenExpiredError':
                return ctx.app.emit('err',tokenERR ,ctx)
            default:
                return ctx.app.emit('err',tokenERR ,ctx)
        }
    }


    await next()

}
module.exports = {
    auth
}